﻿
/////////////////////////////////////////////////////////////////
//File: BlogInfo.cs
//Description: 
//  Detail.aspx
//  
//Copyright© 2011 Moliu
//History:
//  2011-3-13 9:08:40    CodeSmith5.0.1    Original Version
/////////////////////////////////////////////////////////////////

using System;
using System.Threading;
using System.Collections.Generic;
using System.IO;
using MAF.Common;
using My.Blog.Domain.Entities;
using My.Blog.Domain.Interfaces;
using My.Blog.Business.Service;
using My.Blog.Business.Security;
using My.Blog.Web.Controllers;
using System.Configuration;
using System.Collections.Specialized;
using System.Web;

namespace My.Blog.Web.UI_MAF_Template
{
    public class Breadcrumb : My.Blog.Web.Controllers.BasePage
    {
        public Breadcrumb()
        {
            HttpContext context = HttpContext.Current;

            // 标签
            const string IF_EMPTY = "If:Empty";
            const string IF_NOT_EMPTY = "IfNot:Empty";
            const string IF_ARTICLE_DETAIL = "If:ArticleDetail";
            const string LOOP_LIST = "Loop:List";
            const string IF_FIRST = "If:First";
            const string IF_NOT_FIRST = "IfNot:First";

            // 模板路径及逻辑标签正则表达式
            string TPL_FILE_PATH = ConfigurationManager.AppSettings["TemplatesPath"] + @"/Breadcrumb.html";
            const string REG_LOGIC = @"(?is)<(Tag>)(.*?)</\1";

            // 主模板
            string tpl = File.ReadAllText(context.Server.MapPath(TPL_FILE_PATH));
            Template tplLogic = new Template(tpl, REG_LOGIC);
            Template tplLoopEntity;
            Template tplEntity;

            // 数据源
            IList<CategoryInfo> list = new CategoryHandler(this).ReadAncestorsByConditions();// 查找Breadcrumb需要此接口            

            // 文章详细页
            if (!context.Request.Url.GetLeftPart(UriPartial.Path).EndsWith("ArticleDetail.aspx"))
            {
                tplLogic.Replace(IF_ARTICLE_DETAIL, "");
            }

            // 模板替换
            if (0 == list.Count)
            {
                tplLogic.Replace(IF_NOT_EMPTY, "");
                context.Response.Write(tplLogic.Result);
            }
            else
            {
                tplLogic.Replace(IF_EMPTY, "");

                // 替换循环子模板
                tplLoopEntity = new Template(tplLogic.Find(LOOP_LIST).Groups[2].Value);
                IList<NameValueCollection> mapLoop = new List<NameValueCollection>();
                for (int i = 0, len = list.Count; i < len; i++)
                {
                    NameValueCollection nvc;
                    if (0 == i)
                    {
                        nvc = new NameValueCollection();
                        nvc["{Id}"] = "BlogId=" + this.BlogId.ToString();
                        nvc["{Title}"] = "博客";
                    }
                    else
                    {
                        CategoryInfo info = list[i];
                        nvc = MAF.Common.ObjectConverter.ToNameValueCollection(info, @"{Key}", "");
                        nvc["{Id}"] = "CategoryId=" + nvc["{Id}"];
                    }
                    mapLoop.Add(nvc);
                }
                tplLoopEntity.Replace(mapLoop);
                tplLogic.Replace(LOOP_LIST, tplLoopEntity.Result);

                // 替换剩余的实体
                tplEntity = new Template(tplLogic.Result);
                NameValueCollection mapEntity = new NameValueCollection();
                mapEntity.Add(@"{RootUrl}", this.RootUrl);
                tplEntity.Replace(mapEntity);

                context.Response.Write(tplEntity.Result);
            }
        }
    }
}